﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//--------------------------------------------------------------------

using System;
using System.Configuration;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.Windows.Forms;

namespace DotNet.WCFHost
{
    using DotNet.DbUtilities;
    using DotNet.Utilities;

    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // 是否检查在线状态
            BaseSystemInfo.CheckOnLine = false;
            BaseSystemInfo.AllowNullPassword = true;

            // 是否记录日志
            BaseSystemInfo.RecordLog = true;

            // 是否需要注册
            BaseSystemInfo.NeedRegister = false;

            // 读取配置文件
            ConfigHelper.GetConfig();

            System.Console.WriteLine("WCFHost 服务器端已启动。。。");

            // 读取配置文件
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            ServiceModelSectionGroup serviceModelSectionGroup = (ServiceModelSectionGroup)configuration.GetSectionGroup("system.serviceModel");

            // 开启每个服务
            foreach (ServiceElement serviceElement in serviceModelSectionGroup.Services.Services)
            {
                var serviceHost = new ServiceHost(Assembly.Load("DotNet.Service").GetType(serviceElement.Name), serviceElement.Endpoints[0].Address);
                serviceHost.Opened += delegate { Console.WriteLine("{0}", serviceHost.BaseAddresses[0]); };
                serviceHost.Open();
            }

            System.Console.WriteLine("服务器 当前时间：" + DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat));
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            System.Console.WriteLine("数据库服务器 当前时间：" + dbHelper.GetDBDateTime());

            Application.Run(new FormWCFHost());
        }
    }
}